<template>
	<van-form @submit="onSubmit">
		<van-nav-bar title="忘记密码" left-text="" left-arrow @click-left="onClickLeft" />
	  <van-cell-group inset>
	    <van-field
	      v-model="telname"
	      name="用户名"
	      label="用户名"
	      placeholder="请输入用户名"
	      :rules="[{ required: true, message: '请填写用户名' },
		  ]"
	    />
	    <van-field
	      v-model="password"
	      type="password"
	      name="密码"
	      label="密码"
	      placeholder="请输入密码"
	      :rules="[{ required: true, message: '请输入密码' }]"
	    />
		<van-field
		  v-model="re_password"
		  type="password"
		  name="密码"
		  label="密码"
		  placeholder="再次输入密码"
		  :rules="[{ required: true, message: '请再次输入密码' },
		  { validator: checkRePassword, message: '两次密码不一致' }]"
		/>
	  </van-cell-group>
	  <div style="margin: 16px;">
	    <van-button round block type="primary" native-type="submit" >
	      重置密码
	    </van-button>
	  </div>
	</van-form>

</template>

<script lang="ts" setup>
	import { ref } from 'vue';
	import {checkUsername,loginUser} from "@/api/user.ts"
	import { Toast } from 'vant';
	import 'vant/es/toast/style'
	import router from '@/router'
	
	
	const telname = ref('');
	const password = ref('');
	const re_password = ref('');
	const checkRePassword = (val) => {
	console.log("检查两次密码是否一致")
	if(password.value != re_password.value){
	return false
	}
	return true
	}
	const checktelname = (val)=>{
		console.log("检查输入的手机号是否正确")
		if(/^1\d{10}$/.test(val)){
			return true
		}
		return false
	}

	const onSubmit = (data) => {
	  console.log('submit', data);
	  loginUser(data)
	  .then(res=>{
		  if(res.code==0){
			  // 注册成功
			  Toast.success('重置成功');
			  console.log("重置成功")
			  router.push("/login")
		  }
	  })
	  .catch(err=>{
		  // 服务器失败
		  Toast.fail(err);
		  console.log(err)
	  })
	};
	
</script>

<style>
</style>